<?php
namespace app\serverintegralmall\logic;
use \think\Db;
use \app\manage\logic\BaseLogic;


class GoodsCategory extends BaseLogic{
	
	protected $name = 'goods_category';
	protected $config = 'db_config_integralmall';
	protected $goodsCateModel = 'app\serverintegralmall\model\GoodsCategory';
    protected $goodsCateLogic = '\app\serverintegralmall\logic\GoodsCategory';

	use \app\serveruser\traits\addAppId;
    use \app\servergoods\traits\UpdateCate;

	//修改商品分类  
    public function editGoodsCate($data): array
    {
        return $this->_updateCate($this->goodsCateLogic, $this->goodsCateModel, $data, 'dbCateList', 'dbOneCateInfo');
    }

    //添加商品分类
    public function addGoodsCate($data): array
    {
    	$res = $this->model($this->goodsCateModel)->save($data);
    	return auto_return($res);
    }

    //内部db查询某分类信息
    public function dbOneCateInfo($data)
    {
        $_where['id'] = $data['id'];
        $_where['status'] = isset($data['status']) ? $data['status'] : ['neq', -1];
        return $this->db()
                    ->where($_where)
                    ->find();
    }

    /*内部获取不包含公共分类的所有分类列表 查询结果可用于操作*/
    public function dbCateList($map = [])
    {
        isset($map['id']) && $_where['id'] = $map['id'];
        isset($map['pid']) && $_where['pid'] = $map['pid'];
        if(isset($map['app_id'])){
            $isManageAppId = $this->_isManageAppId($map['app_id']);
            !$isManageAppId && $_where['app_id'] = $map['app_id'];
        }
        $_where['status'] = ['neq',-1];
        return $this->db()
                    ->where($_where)
                    ->select();
    }

    /*内部获取包含公共分类的所有分类列表  仅查询不用于操作*/
    public function dbAllCateList($map = [])
    {
        isset($map['id']) && $_where['id'] = $map['id'];
        isset($map['pid']) && $_where['pid'] = $map['pid'];
        $_where['status'] = isset($map['status']) ? $map['status'] : ['neq',-1];

        if(isset($map['app_id'])){   //若卖家查看商品详情，则不传入app_id
            $manageAppId = \app\manage\service\ApiBase::getAppId();
            $_whereOr = $manageAppId == $map['app_id'] ? true : '`app_id`=' . $map['app_id'] . ' OR FIND_IN_SET(' . $map['app_id'] . ',`other_app_read`) OR is_public = 1';
        }else{
            $_whereOr = true;
        }
        if(isset($map['limit']) && $map['limit']){
            $res = $this->db()
                    ->where($_where)
                    ->where($_whereOr)
                    ->limit($map['limit'])
                    ->order('`order` ASC, `id` ASC')
                    ->select();
        }else{
            $res = $this->db()
                    ->where($_where)
                    ->where($_whereOr)
                    ->order('`order` ASC, `id` ASC')
                    ->select();
        }
        return $res;
    }

    /*前台检测该商品分类是否存在可读取 用于发布/编辑商品时的商品分类检测*/
    /*参数： app_id  id */
    public function checkReadCate($data): bool 
    {
        $_where['id'] = $data['id'];
        $_where['status'] = 1;
        $manageAppId = \app\manage\service\ApiBase::getAppId();
        $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        $res = $this->model($this->goodsCateModel)
                    ->where($_where)
                    ->where($_whereOr)
                    ->find();
        return $res ? true : false; 
    }

    //后台获取某商品分类的详细信息
    public function oneCateInfo($data): array
    {
        $_where['id'] = $data['id'];
        $_where['status'] = ['neq',-1];
        if(isset($data['app_id'])){   //若卖家查看商品详情，则不传入app_id
            $manageAppId = \app\manage\service\ApiBase::getAppId();
            $_whereOr = $manageAppId == $data['app_id'] ? true : '`app_id`=' . $data['app_id'] . ' OR FIND_IN_SET(' . $data['app_id'] . ',`other_app_read`) OR is_public = 1';
        }else{
            $_whereOr = true;
        }

        $res = $this->model($this->goodsCateModel)
                    ->where($_where)
                    ->where($_whereOr)
                    ->find();
        $res && $res = $res->toArray();
        return auto_return($res);
    }

    /*
        根据id数组 启用/禁用一个或多个分类
        父类被禁用，则其所有子类也被禁用，开启同理
    */
    public function changeCateStatus($data): array
    {
        $list = $this->dbCateList(['app_id'=>$data['app_id']]);
        $saveData['status'] = $data['status'];

        foreach ($data['id'] as $k => $v) {
            $childId = child_ids($list, $v, 'id', 'pid', 'id');  //使用static数组 获取该分类下的所有子类
        }
        $data['id'] = array_merge($childId, $data['id']);
        $_where['id'] = ['in',$data['id']];
        $_where = $this->_addAppId($_where, $data['app_id']);
        
        $res = $this->model($this->goodsCateModel)->save($saveData, $_where);
        return auto_return($res);
    }

    //后台获取商品分类列表
    public function goodsCateList($data): array
    {
        return $this->_goodsCateList($data);
    }

    //获取商品分类列表
    public function _goodsCateList($data, bool $isFront = false): array 
    {
        $_where = $this->_addAppId([], $data['app_id']);
        $isFront && $_where['status'] = 1;
        $list = $this->dbAllCateList($_where);
        foreach ($list as $k => &$v) {
            $v['status_text'] = $v['status'] ? '正常' : '禁用';
        }
        $list = list_to_tree($list,'id','pid','_child',$data['id']);
        return auto_return($list);
    }
    //删除一个或多个商品分类
    public function delCate($data): array
    {
        $map['pid'] = ['in',$data['id']];
        $map = $this->_addAppId($map, $data['app_id']);

        $list = $this->dbCateList($map);
        if(count($list) > 0){
            return auto_failure(null,'失败，存在子类'.$list[0]['name']);
        }
        $_where['id'] = ['in',$data['id']];
        $_where['status'] = ['in',['0','1']];
        $_where = $this->_addAppId($_where, $data['app_id']);
        $update['status'] = -1;
        $res = $this->db()
                    ->where($_where)
                    ->update($update);
        return auto_return(1,'删除成功','删除失败');
    }

}